package vip.ola.pay.ctrl.payment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.ola.core.common.constant.PayConstant;
import vip.ola.core.common.util.IPUtility;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.common.util.PayUtil;
import vip.ola.core.entity.PayOrder;
import vip.ola.pay.ctrl.common.BaseController;
import vip.ola.pay.service.RpcCommonService;
import javax.servlet.http.HttpServletRequest;
import static vip.ola.core.common.constant.PayConstant.RETURN_VALUE_FAIL;
import static vip.ola.core.common.constant.PayEnum.ERR_0010;

/**
 * @author dingzhiwei jmdhappy@126.com
 * @version V1.0
 * @Description: 支付订单查询
 * @date 2017-08-31

 */
@RestController
public class ResusePayOrderController extends BaseController {

    private final MyLog _log = MyLog.getLog(ResusePayOrderController.class);

    @Autowired
    private RpcCommonService rpcCommonService;

    /**
     * 查询支付订单接口:
     * 1)先验证接口参数以及签名信息
     * 2)根据参数查询订单
     * 3)返回订单数据
     *
     * @param payOrder
     * @return
     */
    @PostMapping(value = "/api/pay/reissueerwd")
    public String queryPayOrder(@RequestBody PayOrder payOrder,HttpServletRequest request) {
        _log.info("###### 开始接收补单订单请求 ######");
        String logPrefix = "【商户支付订单补单】";
        try {
            _log.info("{}请求参数:{}", logPrefix, payOrder);
            // 判断请求IP是否允许+"："
            String clintIp = IPUtility.getClientIp(request);
            _log.info("请求ip为：" + clintIp);
            boolean isAllow = PayUtil.ipAllow(clintIp, "119.23.44.104", "");
            if (!isAllow) {
                _log.info( "IP[" + clintIp + "]不允许访问");
                return PayUtil.makeRetFail(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "ip fail", null, null));
            }
            PayOrder exist = rpcCommonService.rpcPayOrderService.findByPayOrderId(payOrder.getPayOrderId());
            if (exist!= null){

            }else {
              int  result = rpcCommonService.rpcPayOrderService.createPayOrder(payOrder);
                if (result != 1) {
                    return PayUtil.makeRetFail(PayUtil.makeRetMap(RETURN_VALUE_FAIL, "补单失败", null, ERR_0010.getCode(), "DB插入支付订单失败"));
                }
            }

            // 是否通知商户
            boolean isNotifyMch = false;
            if(payOrder.getStatus() == PayConstant.PAY_STATUS_PAYING) { // 初始或支付中
                // 修改状态为支付成功,
                int updateCount = rpcCommonService.rpcPayOrderService.updateStatus4Success(payOrder.getPayOrderId());
                _log.info("[补单]payOrderId={},将支付中修改为支付成功,返回结果:{}",  payOrder.getPayOrderId(), updateCount);
                if(updateCount == 1) {
                    isNotifyMch = true;
                }
            }

            // 发送商户通知
            if(isNotifyMch) {
                rpcCommonService.rpcPayNotifyService.executePayNotify(payOrder.getPayOrderId());
            }
            _log.info("###### 商户补单处理完成 ######");
            return PayUtil.makeRetSuc().toJSONString();
        } catch (Exception e) {
            _log.error(e, "");
            return PayUtil.makeRetFail(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "支付中心系统异常", null, null));
        }
    }




}
